package com.adinnet.api.school.controller;


import com.adinnet.api.school.model.School;
import com.adinnet.api.school.service.SchoolService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResp;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 校区 前端控制器
 * </p>
 *
 * @author liukp
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/api/school")
public class SchoolController {


    @Autowired
    private SchoolService schoolService;

    /**
     * 校区列表
     *
     * @return
     */
    @GetMapping("list")
    public PageResp schoolList(Integer limit, Integer page, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (null == limit) {
                return new PageResp("limit不能为空");
            }
            if (null == page) {
                return new PageResp("page不能为空");
            }
            IPage<School> schoolList = schoolService.schoolList(limit, page);
            if (null == schoolList || CollectionUtils.isEmpty(schoolList.getRecords())) {
                return new PageResp(Lists.newArrayList());
            }
            return new PageResp(schoolList.getRecords(), schoolList.getTotal());
        } catch (BizException e) {
            return new PageResp(e.getMessage());
        } catch (Exception e) {
            return new PageResp("查询校区列表失败!");
        }
    }

}
